
<template>
    <div class="model-form">
        <el-page-header @back="goBack" content="更新电池">
        </el-page-header>
        <div class="model-content">
            <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
                <el-row :gutter="10">
    <el-col :span="12">
            <el-form-item label="电池 SN"  prop="batterySN">
        <el-input v-model="form.batterySN" name="batterySN">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="型号"  prop="batteryModel">
        <el-select style="width: 100%;" v-model="form.batteryModel" filterable clearable
                   placeholder="请选择">
            <el-option v-for="item in batteryModelOptions" :key="item.id"
                       :label="item.name" :value="item.id">
            </el-option>
        </el-select>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="状态"  prop="status">
        <el-input v-model="form.status" name="status">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="联网状态"  prop="onlineState">
        <nb-select catalog="onlineState" v-model="form.onlineState"></nb-select>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="归属"  prop="ownership">
        <el-input v-model="form.ownership" name="ownership">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="SOC"  prop="soc">
        <el-input v-model="form.soc" name="soc">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="充电状态"  prop="chargingStatus">
        <el-input v-model="form.chargingStatus" name="chargingStatus">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="放电状态"  prop="dischargingStatus">
        <el-input v-model="form.dischargingStatus" name="dischargingStatus">
        </el-input>
            </el-form-item>

    </el-col>

                    <el-col :span="22">
                        <el-form-item>
                            <el-button @click="goBack">取消</el-button>
                            <el-button type="primary" @click="updateData">确定</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </div>
</template>

<script>
    import common from "@/mixins/common.js";

    import nbSelect from "@/components/nbSelect.vue";

    export default {
        name: "battery_update",
        mixins: [common],
        components: {
            nbSelect
        },
        data() {
            return {
                form: {
                    batterySN: '',
                    batteryModel: '',
                    status: '',
                    onlineState: '',
                    ownership: '',
                    soc: '',
                    chargingStatus: '',
                    dischargingStatus: ''                },
                batteryModelOptions: [],
                rules: {
                    batterySN: [
                        {required: true, message: '请输入电池 SN', trigger: 'blur'}
                    ],
                }
            }
        },
        async mounted() {
            let id = this.$route.query.id;
            let data = {};
            data.id = id;
            let res = await this.$http.form("/battery/view", data);
            if (res.code === 200) {
                this.form = res.data;
            }
            this.loadBatteryModelOptions();
        },
        methods: {
            async updateData() {
                try {
                    let valid = await this.$refs["ruleForm"].validate();
                    if (!valid) {
                        return;
                    }
                } catch (e) {
                    return;
                }
                await this.updateDataPost();
            },
            async updateDataPost() {
                let res = await this.$http.form("/battery/update", this.form);
                if (res.code !== 200) {
                    this.$message.error(res.msg);
                    return
                }
                this.$message({
                    message: '更新数据成功',
                    type: 'success'
                });
                this.$router.go(-1);
            },
            async loadBatteryModelOptions() {
                let self = this;
                let param = {};
                param.sortMethod = "asc";
                param.sortField = "id";
                param.level = 1;
                param.size = 500;
                param.fetch = 0;
                let res = await this.$http.form("/batteryModel/list", param);
                if (res.code === 200) {
                    self.batteryModelOptions = res.data;
                }
            },

        }
    }


</script>

<style scoped>

</style>